HEAD 💀
HEAD
HEAD
is just a special pointer which points to current state (commit) of our repo.
Reflog
reflog
is a special log
which only stores the moving history of HEAD
pointer.
Why it is useful ?
This will be an interesting set of tasks to do, but can be quite useful in understanding reflog.
- create a new branch off of
master
, call itbaz
. - add one commit to
baz
. Do it in a new filebaz.md
- switch back to
master
and deletebaz
(git branch -D baz from earlier) - can you bring back from the dead the commit sha of
baz
?
Moving HEAD
We can also move HEAD
relative to its position, to walk back commits.
We use a special syntax with ~n
to give relative position of commits wrt to current commit.
git checkout HEAD~3
Reset
We can reset current state of repo to some previous state.
there are 2 options to reset to a previous state
git reset --hard <SHA>
# or
git reset --soft <SHA>
--hard
: will reset your repo state to previous commit, and also changes will be lost. ("😏 except from reflog
")
--soft
: will reset your repo state to previous commit, but changes will reflect back into tracked stage.
Revert
Revert is like anti-commit of <provided_commit>
, basically it will not change any history but will add an anti-change which will remove the changes of the <provided_commit>
.
git revert <SHA>
Clean
git reset
is good to move HEAD
to some previous state, but what about we want to clean our repo to its last commit. Basically we want to discard the changes.
to do that we use
git clean -f -d -x
Restore
To restore some changes in a file.
git restore <file_name>
cherry-pick 🍒
We can bring only one commit from one branch to another branch ny cherry picking that commit.
git cherry-pick <commit SHA>
cherry-pick is one of the most useful command of git, because there will certainly be cases where a change has such an bad divergence and the merge conflict is so bad, that only thing is to hand move (cherry-pick) the commits across branches.